$(document).ready(function() {
   initMap();
   getQuestion();
});

//마커아이콘 var 설정
//var icon = new daum.maps.MarkerImage(
//				'./img/icons.png',
//				new daum.maps.Size(31, 34),
//				new daum.maps.Point(16,34),
//				"poly",
//				"1,20,1,9,5,2,10,0,21,0,27,3,30,9,30,20,17,33,14,33"
//				);
//
function initMap(){
	var map;
	map = new daum.maps.Map(document.getElementById('map'), {
		center: new daum.maps.LatLng(37.537123, 127.005523),
		level: 4
	});

	var zoomControl = new daum.maps.ZoomControl();
	map.addControl(zoomControl, daum.maps.ControlPosition.RIGHT);
	var mapTypeControl = new daum.maps.MapTypeControl();
	map.addControl(mapTypeControl, daum.maps.ControlPosition.TOPRIGHT);

	/*roadview control*/
		var roadviewControl = document.createElement("div");

		roadviewControl.id = "btnRVControl";

		roadviewControl.style.backgroundImage= "url('img/btn_rv.png')";
		roadviewControl.style.width = "77px";
		roadviewControl.style.height = "27px";
		roadviewControl.style.cursor = "pointer";
		roadviewControl.style.marginTop = "-5px";
		roadviewControl.style.marginLeft = "-13px";
		
		roadviewControl.onclick = function(ev) {
			rvSubmit();
			showRoadView();
		};
	
		map.addControl(roadviewControl, daum.maps.ControlPosition.TOPRIGHT);

		var marker = null;

		daum.maps.event.addListener(map,"click",function(MouseEvent){
			var latLng = MouseEvent.latLng;
			var latitude = latLng.getLat();
			var longitude = latLng.getLng();
			
			$("#latitude").val(latitude);
			$("#longitude").val(longitude);			
			
			if(marker==null)
			{
				marker = new daum.maps.Marker({
				position: new daum.maps.LatLng(37.537123, 127.005523)
				});
				//아이콘을 추가한 마커
				//marker = new daum.maps.Marker({
				//position: new daum.maps.LatLng(37.537123, 127.005523),
				//image: icon
				//});
				
				
				marker.setMap(map);
				marker.setVisible(true);

				daum.maps.event.addListener(marker, 'click',(function(){			
				var marlatLng = marker.getPosition();
				var marlatitude = marlatLng.getLat();
				var marlongitude = marlatLng.getLng();
				if(confirm("Road View?")){
			
					var p = new daum.maps.LatLng(marlatitude, marlongitude);
					var rc = new daum.maps.RoadviewClient();
					var rv = new daum.maps.Roadview(document.getElementById("roadview"));

					rc.getNearestPanoId(p, 50, function(panoid) {
						rv.setPanoId(panoid, p);
						rv.setViewpoint({ pan: 15, tilt: 40, zoom: 2 });
					});

				}else{	
					
				}

				}
				));
			}
		
			marker.setPosition(latLng);			
		});
}

function SendQuestion(){
    $.getJSON('/ajax.php',
	      {
                 "ajax_method":"INSERT_QUESTION",
                 "msg":encodeURI($('#message').val()),
                 "latitude":$("#latitude").val(),
                 "longitude":$("#longitude").val()
              }, 
              function(data) {
         $.each(data, function(key, val) {
               if (key == 'true') {
		   writeYozm($('#message').val());
                   getQuestion();
               } else if (key == 'false') {
                   alert(val);
               }
         });
    });
}

function writeYozm(msg){
    $.getJSON('/ajax.php',
	    {
                 "ajax_method":"SEND_YOZM",
                 "msg":encodeURI(msg)
            }, 
            function(data) {
		if(data.status != 200){
			alert(data.result_msg);
		}
            }
    );
}

function getQuestion(){
    $('#questions').html("");

    $.getJSON('/ajax.php?ajax_method=GET_QUESTION&page=1&rows=10', function(question_ajax) {
         $.each(question_ajax, function(key, val) {
              var html_val = "";
	      html_val = html_val + "<table width='100%'><tr><td><div style='float:left;color:blue;text_weight:bold;'>질문 : "+val.msg+"&nbsp;</div>";
              html_val += "<div id='arrow_"+val.qid+"' onClick='answerArea("+val.qid+")' style='float:left;'>-&gt</div></td></tr></table>";
              html_val += "<div id='answer_form_"+val.qid+"'></div>";
              html_val += "<div id='answer_"+val.qid+"'>";

              html_val += "</div>";
              $('#questions').append(html_val);

              var answer_html = "";
	      $.getJSON('/ajax.php?ajax_method=GET_ANSWER&qid='+val.qid, function(answer_ajax) {
		 $.each(answer_ajax, function(key1, val1) {
		      answer_html = answer_html + "<div class='answer'>&nbsp;&nbsp;답: "+val1.msg+"&nbsp;</div>";
                 });
                  $('#answer_'+val.qid).html(answer_html);
              });
         });
    });
}

function answerArea(qid){
    var answer_form  = "";

    if($("#arrow_"+qid).html() == "-&gt;"){
	    $("#arrow_"+qid).html("+");
            var answer_form =  "&nbsp;&nbsp;<input type='textbox' id='answer_text_"+qid+"' class='insert_answer' value=''/>";
                answer_form +=  "<input type='button' value='답변하기' onClick='sendAnswer("+qid+")'/>";
    }else{
	    $("#arrow_"+qid).html("-&gt;");
            var answer_form =  "";
    }
    $("#answer_form_"+qid).html(answer_form);
}

function sendAnswer(qid){
    $.getJSON('/ajax.php?ajax_method=INSERT_ANSWER&qid='+qid+'&msg=' + encodeURI($("#answer_text_"+qid).val()), function(data) {
         $.each(data, function(key, val) {
               if (key == 'true') {
                   getQuestion();
               } else if (key == 'false') {
                   alert(val);
               }
         });
    });
}
